﻿using UnityEngine;
using System.Collections;
using System.Collections.Generic;

public enum TileType
{
	TILE_32x32,
	TILE_64x64

};

public class TileGenerator : MonoBehaviour 
{
	public string[,] m_tileNumbers;
	public CSVReader csvReader;
	private float m_tileWidth = 0.0f;
	private float m_tileHeight = 0.0f;
	public GameObject[] m_tileObjects = new GameObject[12];
	public List<GameObject> m_tileCollection;
	public Camera m_mainCamera;
	public float m_playerPosX = 0.0f;
	public float m_playerPosY = 0.0f;
	public TileType m_tileType;
	// Use this for initialization
	void Start () 
	{

	}
	public void Initialize()
	{
		csvReader.Parse(m_tileType);
		m_tileNumbers = csvReader.GetGrid();

		if( m_tileType == TileType.TILE_32x32 )
		{
			m_tileWidth = 32.0f;
			m_tileHeight = 32.0f;
		}
		else
		{
			m_tileWidth = 64.0f;
			m_tileHeight = 64.0f;
		}
		GenerateTiles();
	}
	// Update is called once per frame
	void Update () 
	{

	}

	void GenerateTiles()
	{
		DestroyTiles ();
		float fXpos = 0 + (m_mainCamera.transform.position.x - m_mainCamera.pixelRect.width * 0.5f);
		float fYpos = (m_mainCamera.transform.position.y - m_mainCamera.pixelRect.height * 0.5f) + Screen.height - m_tileHeight; 
		/*Debug.Log("Screen Width : "+ Screen.width + " Screen Height : " + Screen.height);
		Debug.Log("fXpos : " + fXpos + " fYpos : " + fYpos);
		Debug.Log("Tile Width : " + m_tileWidth + " Tile Height : " + m_tileHeight);
		Debug.Log("Array Width : " + m_tileNumbers.GetUpperBound(0) + " Array Height : " + m_tileNumbers.GetUpperBound(1));
		Debug.Log("----------------------------------------------------------------------------");*/
		int i = 0, j = 0;
		string eof = "Z", eol = "X";
		int indexOff = 0;
		if( m_tileType == TileType.TILE_32x32 )
			indexOff = 5;

		while( true )
		{
			int tileNum = -1, number = -1;
			if( m_tileNumbers[i,j].Equals(eof) )
			{
				break;
			}
			else
			{
				if( m_tileNumbers[i,j].Equals(eol) )
				{
					//END of the line
					//Start a new Row
					i++;
					j = 0;
					//Debug.Log("===> ROW : " + i);
					fYpos -=  + m_tileHeight;
					fXpos = 0.0f + (m_mainCamera.transform.position.x - m_mainCamera.pixelRect.width * 0.5f);
				}
				else
				{
					if (int.TryParse(m_tileNumbers[i,j], out number))
					{
						if( number == 0 )
						{
							number = 1;
							m_playerPosX = fXpos;
							m_playerPosY = fYpos;
						}
						tileNum = number - 1;
						//Debug.Log("X Pos : " + fXpos + " Y Pos : " + fYpos);
						if( tileNum == 0 || tileNum == 3 )
						{
							//Grass or Non Breakable Tile
							GameObject tile = Instantiate(m_tileObjects[tileNum +indexOff], new Vector3(fXpos, fYpos, 0.0f), Quaternion.identity) as GameObject;
							tile.name = "Tile " + (i + 1) + " , " + ( j + 1); 
							fXpos += m_tileWidth;
							m_tileCollection.Add( tile );
							j++; 
						}
						else if( tileNum == 1 )
						{
							//Damaged Brick Tile
							GameObject tile = Instantiate(m_tileObjects[2+indexOff], new Vector3(fXpos, fYpos, 0.0f), Quaternion.identity) as GameObject;
							tile.name = "DamagedTile " + (i + 1) + " , " + ( j + 1); 
							//Brick Tile
							tile = Instantiate(m_tileObjects[tileNum + indexOff], new Vector3(fXpos, fYpos, 0.0f), Quaternion.identity) as GameObject;
							tile.name = "Tile " + (i + 1) + " , " + ( j + 1); 
							fXpos += m_tileWidth;
							m_tileCollection.Add( tile );
							j++;
						}
						//Debug.Log(i + "," +j+" = " + m_tileNumbers[i,j] + " TileNum: " + tileNum);
					}
					else
					{
						Debug.Log("String : " + m_tileNumbers[i,j] );
					}
				}
			}
						
		}
	}

	void DestroyTiles()
	{
		for (int i = 0; i < m_tileCollection.Count; i++) 
		{
			Destroy(m_tileCollection[i]);
			m_tileCollection[i] = null;
		}
		m_tileCollection.Clear ();
	}
}
